package com.sutuijingling.bean;

import com.sutuijingling.result.c;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.http.HttpStatus;

public class b {
    static final char[] a = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    private static final c b = c.MD5;

    public static String a(String str) {
        int i;
        String stringBuilder;
        int length = str.length();
        if (length < 16) {
            int i2 = 16 - length;
            StringBuilder stringBuilder2 = new StringBuilder(str);
            for (i = 0; i < i2; i++) {
                stringBuilder2.append('0');
            }
            stringBuilder = stringBuilder2.toString();
        } else {
            stringBuilder = str;
        }
        if (length <= 16) {
            return stringBuilder;
        }
        StringBuilder stringBuilder3 = new StringBuilder(stringBuilder);
        for (i = length; i > 16; i--) {
            stringBuilder3.deleteCharAt(i - 1);
        }
        return stringBuilder3.toString();
    }

    public static String a(String str, String str2) throws c {
        try {
            Key secretKeySpec = new SecretKeySpec(a(str2).getBytes(), "AES");
            Cipher instance = Cipher.getInstance("AES");
            instance.init(1, secretKeySpec);
            return a.a(instance.doFinal(str.getBytes()));
        } catch (Exception e) {
            throw new c(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
        }
    }

    public static String b(String str, String str2) throws c {
        try {
            String a = a(str2);
            System.out.println("value:[" + str + "] private key:" + a + " length:[" + a.length() + "]");
            Key secretKeySpec = new SecretKeySpec(a.getBytes(), "AES");
            Cipher instance = Cipher.getInstance("AES");
            instance.init(2, secretKeySpec);
            return new String(instance.doFinal(a.a(str)));
        } catch (Exception e) {
            e.printStackTrace();
            throw new c(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
        }
    }
}
